#include <cstdio>

int f[ 201 ][ 201 ], n, m;

void init( )
{
    int i, j, a, b, c;
    for ( i = 0; i < n; i++ )
    {
        for ( j = 0; j < n; j++ )
            f[ i ][ j ] = 0xfffffff;
        f[ i ][ i ] = 0;
    }
    for ( i = 0; i < m; i++ )
    {
        scanf("%d%d%d", &a, &b, &c);
        if ( f[ a ][ b ] > c )
        {
            f[ a ][ b ] = c;
            f[ b ][ a ] = c;
        }
    }
}

void work( )
{
    int i, j, k;
    for ( k = 0; k < n; k++ )
        for ( i = 0; i < n; i++ )
        {
            if ( i == k ) continue;
            for ( j = 0; j < n; j++ )
            {
                if ( j == i || j == k ) continue;
                if ( f[ i ][ k ] + f[ k ][ j ] < f[ i ][ j ] )
                    f[ i ][ j ] = f[ i ][ k ] + f[ k ][ j ];
            }
        }
    scanf("%d%d", &i, &j);
    if ( f[ i ][ j ] == 0xfffffff )
        printf("-1\n");
    else
        printf("%d\n", f[ i ][ j ]);
}

int main( )
{
    while ( scanf("%d%d", &n, &m) != EOF )
    {
        init( );
        work( );
    }
    return 0;
}
